package com.dqhl.smartstable.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseFragment;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by mac on 16/10/13.
 * 位置
 */
public class LocationFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv_top_center;
    private ImageView iv_msg;
    private String strSN;
    private String token;

    private MapView mapView;
    private AMap aMap;
    private View mapLayout;
    private double curLongitude; //经度
    private double curLatitude; //纬度
    private String data;

    private AMapLocationClient mLocationClient = null;
    public AMapLocationListener mLocationListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mapLayout == null) {
            Dlog.e("MF onCreateView() null");
            mapLayout = inflater.inflate(R.layout.fragment_location, container, false);
            mapView = (MapView) mapLayout.findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);
            if (aMap == null) {
                aMap = mapView.getMap();
            } else {
                if (mapLayout.getParent() != null) {
                    ((ViewGroup) mapLayout.getParent()).removeView(mapLayout);
                }
            }
            mLocationClient = new AMapLocationClient(app.getApplicationContext());
            mLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {

                }
            });

            initView(mapLayout);
            getCaseLocation();
        }


        return mapLayout;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    private void initView(View view) {
        tv_top_center = (TextView) view.findViewById(R.id.tv_top_center);
        tv_top_center.setText("位 置");
        iv_msg = (ImageView) view.findViewById(R.id.iv_msg);

        iv_msg.setOnClickListener(this);
    }

    /**
     * 获取箱子位置
     */
    private void getCaseLocation() {
        showCustomLoadBar("获取箱子位置中");
        strSN = sp.getString(Constant.SN, "");
        token = sp.getString(Constant.TOKEN, "");
        Dlog.e("case_sign:" + strSN + ";token:" + token);
        RequestParams params = new RequestParams(Config.Api.showpoint);
        params.addBodyParameter("case_sign", strSN);
        params.addBodyParameter("token", token);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    String location[] = data.split(",");
                    curLatitude = Double.parseDouble(location[0]);
                    curLongitude = Double.parseDouble(location[1]);
                    LatLng latLng = new LatLng(curLongitude, curLatitude);
                    Dlog.e(curLongitude + "," + curLatitude);

                    mUiSettings = aMap.getUiSettings();
                    mUiSettings.setZoomControlsEnabled(false);
                    mUiSettings.setCompassEnabled(true);
                    mUpdata = CameraUpdateFactory.newCameraPosition(//15是缩放比例，0是倾斜度，30显示比例
                            new CameraPosition(latLng, 10, 0, 30));//这是地理位置，就是经纬度。
                    aMap.moveCamera(mUpdata);//定位的方法
                    final Marker marker = aMap.addMarker(new MarkerOptions().
                            position(latLng).
                            snippet("DefaultMarker"));
//                marker.showInfoWindow();// 设置默认显示一个infowinfow
                } else {
                    mLocationClient.startLocation();
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    private UiSettings mUiSettings;
    private CameraUpdate mUpdata;

//    private void init() {
//        if (aMap == null) {
//            aMap = mapView.getMap();
//            mUiSettings = aMap.getUiSettings();
//            mUiSettings.setZoomControlsEnabled(false);
//            mUiSettings.setCompassEnabled(true);
//            mUpdata = CameraUpdateFactory.newCameraPosition(//15是缩放比例，0是倾斜度，30显示比例
//                    new CameraPosition(new LatLng(40.043212, 116.299728), 15, 0, 30));//这是地理位置，就是经纬度。
//            aMap.moveCamera(mUpdata);//定位的方法
//            drawMarkers();
//        }
//    }

    public void drawMarkers(LatLng latLng) {

        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("八维")
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .draggable(true));
        marker.showInfoWindow();// 设置默认显示一个infowinfow
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_msg:
                toast("暂无消息");
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mLocationClient != null){
            mLocationClient.stopLocation();
        }
    }
}
